
function quitOutVideo() {
    video_p2p.hidden=video_me.hidden=true;isInVideo=false;//设置状态
    video_list.text('');
    if(mediaStreamTracks) {
        mediaStreamTracks.forEach((track)=>track.stop());
        mediaStreamTracks=null;
    }
    for(let id in pcs) {
        pcs[id].close();
        delete pcs[id];
    }
}
// 创建点对点连接
async function addInVideoRTC(isMulti) {
    if (isMulti) {

    } else {
        pcs[chatting_user]=createRTCPeerConnection(chatting_user,video_p2p,true);
        if(pcs[chatting_user]) video_list.show();
    }
}
async function createRTCPeerConnection(user_id,remoteVideo,isoffer) {
    let localStream;
    if (isoffer) {
        try {
            localStream = await navigator.mediaDevices.getUserMedia({audio: true, video: true});
            video_me.srcObject = localStream;
            video_p2p.hidden = video_me.hidden = false;
            isInVideo = true;//设置状态
            mediaStreamTracks = localStream.getTracks();
        } catch (err) {
            quitOutVideo();
            alert('获取流失败！');
            console.error(err);
            return;
        }
    }
    pcs[user_id] = new PeerConnection(configuration);
    console.log(user_list[user_id] + '>>> 新建一个pc');
    pcs[user_id].onicecandidate = ({candidate}) => iosocket.emit('webrtc',{candidate:candidate,id: user_id});
    // if(mkoffer)
    pcs[user_id].onnegotiationneeded = async () => {
        try {
            await pcs[user_id].setLocalDescription(await pcs[user_id].createOffer());
            // send the offer to the other peer
            iosocket.emit('webrtc',{sdp: pcs[user_id].localDescription,id:user_id});
            console.log('发送offer！');
        } catch (err) {
            console.log('sdp创建失败！');//alert('发起方sdp创建失败！');
            console.error(err);
        }
    };
    pcs[user_id].ontrack = (event) => {
        // don't set srcObject again if it is already set.
        console.log('添加对方的流');
        // if (remoteVideo.srcObject) return;
        remoteVideo.srcObject = event.streams[0];
    };
    if(isoffer) localStream.getTracks().forEach(track => pcs[user_id].addTrack(track, localStream));
    else console.log('接受者初始化！');
    return true;
}